package LeetCode;

public class No238 {
    public static void main(String[] args) {
        int[] nums = {-1,1,0,-3,3};
//        for (int i = 0; i < nums.length; i++) {
//            System.out.print(nums[i]+" ");
//        }
//        System.out.println();
        productExceptSelf(nums);
    }

    public static int[] productExceptSelf(int[] nums) {
        int[] result1 = new int[nums.length];
        int[] result2 = new int[nums.length];
        int[] result3 = new int[nums.length];
        int sum=1;
        for (int i = 0; i < nums.length; i++) {
            sum*=nums[i];
            result1[i]=sum;
        }
        sum =1;
        for (int i = nums.length-1; i>0; i--) {
            sum*=nums[i];
            result2[i]=sum;
        }
//        for (int i = 0; i < result1.length; i++) {
//            System.out.print(result1[i]+" ");
//        }
//        System.out.println();
//        for (int i = 0; i < result2.length; i++) {
//            System.out.print(result2[i]+" ");
//        }
//        System.out.println();
        for (int i = 0; i < nums.length; i++) {
            if(i==0){
                result3[i]=result2[i+1];
            }else if(i== nums.length-1){
                result3[i]=result1[i-1];
            }else{
                result3[i]=result1[i-1]*result2[i+1];
            }
        }
//        for (int i = 0; i < result3.length; i++) {
//            System.out.print(result3[i]+" ");
//        }
        return result3;
    }
}
